package com.hm.cms.service;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.cms.auto.SystemEnvConfigProperties;
import com.hm.cms.domain.QArticleContentDomain;
import com.hm.cms.domain.QArticleDomain;
import com.hm.cms.repository.ArticleContentRepository;
import com.hm.cms.repository.ArticleRepository;
import com.hm.cms.repository.TermTaxonomyRepository;
import com.hm.cms.repository.TermsRepository;
import com.hm.cms.service.BoosSerice;
import com.hm.common.su.bean.PageInfo.PageParam;
import com.hm.common.util.FileUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 下午3:42:45 2017年10月13日
 * @version 1.0
 * @describe
 */
@Slf4j
@Service
@SuppressWarnings("unused")
public class BoosSericeImpl implements BoosSerice {

	@Autowired
	private ArticleRepository articleRepository;

	@Autowired
	private ArticleContentRepository articleContentRepository;

	@Autowired
	private TermsRepository termsRepository;

	@Autowired
	private TermTaxonomyRepository termTaxonomyRepository;

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	/**
	 * @param pageParam
	 * @param cover
	 *            覆盖
	 */
	@Override
	public void staticizeArticle(PageParam pageParam, boolean cover) {
		QArticleDomain qArticleDomain = QArticleDomain.articleDomain;
		Pageable pageable = new QPageRequest(pageParam.getPage(), pageParam.getSize(),
				qArticleDomain.createTime.desc());

		articleRepository.findAll(pageable).getContent().parallelStream().forEach(article -> {
			Optional.ofNullable(article.getStaticizeUri()).ifPresent(staticizeUrl -> {
				File file = new File(article.getStaticizeUri());
				if (file.exists()) {
					return;
				}
			});

			Optional.ofNullable(articleContentRepository
					.findOne(QArticleContentDomain.articleContentDomain.articleId.eq(article.getId())))
					.ifPresent(articleContent -> {

						try {
							FileUtil.writer(staticizePath(article.getId()), "");
						} catch (IOException e) {
							log.error(e.getMessage(), e);
						}
					});
		});
	}

	private String staticizePath(String articleId) {
		return null;
	}
}
